﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.OracleClient;
using Xiaov.SmartWeb.Application.SupCan;

namespace Xiaov.SmartWeb.Application.HXZB
{
    public partial class JjzbIndexData : System.Web.UI.Page
    {
        protected string nf = DateTime.Now.Year.ToString();
        protected string yf = (DateTime.Now.Month - 1).ToString();
        protected string dwbm = "HB"; 
        protected void Page_Load(object sender, EventArgs e)
        {
            nf = Request.QueryString["nf"];
            yf = Request.QueryString["yf"];
            dwbm = Request.QueryString["dwbm"];

            InitTable();
        }
        protected void InitTable()
        {            
            string sbSql = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("zbmc");
            dt.Columns.Add("bysj");
            dt.Columns.Add("bnlj");
            dt.Columns.Add("qntqlj");
            dt.Columns.Add("zbbm");
            #region 主营业务收入
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820006'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820006' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            OracleDataReader dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务收入";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务收入";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 主营业务成本
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820007'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820007' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务成本";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务成本";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 主营业务税金及附加
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820008'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820008' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务税金及附加";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "主营业务税金及附加";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 营业费用
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820009'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820009' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "营业费用";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "营业费用";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 管理费用
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820010'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820010' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "管理费用";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "管理费用";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 财务费用
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820011'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820011' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "财务费用";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "财务费用";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            #region 利润总额
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
(select * from tg82 where zbbm='TG820013'and nf={1} and yf={0}-1 and dwbm={2}) s 
WHERE t.zbbm='TG820013' and t.nf={1} and t.yf={0} and t.dwbm={2} and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "'HB'"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "利润总额";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "利润总额";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 转换成XML
            string dataString = ConfigurationManager.AppSettings["JjzbIndex_Column"];
            ConvertToXml.Convert(dt, dataString, 0, dt.Rows.Count); 
            #endregion
        }
    }
}